﻿using System.Collections.Generic;
using System.Windows;
using Bridge.Base.Constructs;
using System;

namespace Bridge.Objects
{
    /// <summary>
    /// View for a ConverterProperty.
    /// </summary>
    public class ConverterPropertyView : ChangableObject
    {
        /// <summary>
        /// Default constructor for a ConverterPropertyView.
        /// </summary>
        /// <param name="converterProperty">ConverterProperty to show as a view.</param>
        public ConverterPropertyView(ConverterProperty converterProperty)
        {
            if (converterProperty == null) throw new ArgumentNullException("converterProperty");

            mConverterProperty = converterProperty;
        }

        /// <summary>
        /// Gets the Visibility of the TextBox.
        /// </summary>
        public Visibility TextBoxVisibility
        {
            get { return mConverterProperty.PropertyType == PropertyType.String || mConverterProperty.PropertyType == PropertyType.Integer ? Visibility.Visible : Visibility.Collapsed; }
        }

        /// <summary>
        /// Gets the Visibility of the CheckBox.
        /// </summary>
        public Visibility CheckBoxVisibility
        {
            get { return mConverterProperty.PropertyType == PropertyType.Boolean ? Visibility.Visible : Visibility.Collapsed; }
        }

        /// <summary>
        /// Gets the Visibility of the ChoiceBox.
        /// </summary>
        public Visibility ChoiceBoxVisibility
        {
            get { return mConverterProperty.PropertyType == PropertyType.Choice ? Visibility.Visible : Visibility.Collapsed; }
        }

        /// <summary>
        /// Gets the Visibility of the FileBox.
        /// </summary>
        public Visibility FileBoxVisibility
        {
            get { return mConverterProperty.PropertyType == PropertyType.File ? Visibility.Visible : Visibility.Collapsed; }
        }

        /// <summary>
        /// Gets the key name of the property.
        /// </summary>
        public string Key
        {
            get { return mConverterProperty.Key; }
        }

        /// <summary>
        /// Gets the choices that the user can choose from.
        /// </summary>
        public List<string> PropertyChoices
        {
            get { return mConverterProperty.PropertyChoices; }
        }

        /// <summary>
        /// Gets the value of the property.
        /// </summary>
        public object Value
        {
            get
            {
                switch (mConverterProperty.PropertyType)
                {
                    case PropertyType.Boolean:
                        return mConverterProperty.Value != null && mConverterProperty.Value.Equals(true);
                    case PropertyType.Choice:
                    case PropertyType.File:
                    case PropertyType.Integer:
                    case PropertyType.String:
                        return mConverterProperty.Value != null && mConverterProperty.Value is string ? mConverterProperty.Value.ToString() : null;
                }
                return null;
            }
            set
            {
                mConverterProperty.Value = value;
                NotifyPropertyChanged("Value");
                NotifyPropertyChanged("IsValid");
                NotifyPropertyChanged("StringValue");
                NotifyPropertyChanged("BoolValue");
            }
        }

        /// <summary>
        /// Gets the value of the property in string format.
        /// </summary>
        public string StringValue
        {
            get
            {
                return mConverterProperty.Value != null && mConverterProperty.Value is string ? mConverterProperty.Value.ToString() : null;
            }
            set
            {
                mConverterProperty.Value = value;
                NotifyPropertyChanged("Value");
                NotifyPropertyChanged("StringValue");
                NotifyPropertyChanged("IsValid");
            }
        }

        /// <summary>
        /// Gets the value of the property in bool format.
        /// </summary>
        public bool BoolValue
        {
            get { return mConverterProperty.Value != null && mConverterProperty.Value.Equals(true); }
            set
            {
                mConverterProperty.Value = value;
                NotifyPropertyChanged("Value");
                NotifyPropertyChanged("BoolValue");
                NotifyPropertyChanged("IsValid");
            }
        }

        /// <summary>
        /// Gets if this property is valid.
        /// </summary>
        public bool IsValid
        {
            get
            {
                if (mConverterProperty.Validator == null)
                {
                    return true;
                }
                else
                {
                    return mConverterProperty.Validator.Invoke(Value);
                }
            }
        }

        /// <summary>
        /// Gets the ConverterProperty this is associated with.
        /// </summary>
        public ConverterProperty ConverterProperty
        {
            get { return mConverterProperty; }
        }

        private ConverterProperty mConverterProperty;
    }
}
